package com.qumitech.visualnovel.component.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用于解析agent数据
 * agent格式"GEEKFOOTBALL/Android/2.0.2;80687dd140627c8f(Meizu PRO 7 Plus;Android7.0)"
 */
public class ParseAgentUtil {

    public static String getVendor(String agent) {
        if (StringUtils.isEmpty(agent)) {

            return null;
        }
        Pattern pattern = Pattern.compile("\\(.*?;");
        Matcher matcher = pattern.matcher(agent);
        if (matcher.find()) {

            String findContent = matcher.group();
            // 移除头部(和尾部;符号
            String deviceType = findContent.substring(1, findContent.length() - 1);
            if (deviceType.contains(" ")) {

                return deviceType.split(" ")[0].toLowerCase();
            }
        }
        return null;
    }

    public static String getDeviceInfo(String agent) {
        if (StringUtils.isEmpty(agent)) {

            return null;
        }
        Pattern pattern = Pattern.compile("\\(.*?\\)");
        Matcher matcher = pattern.matcher(agent);
        if (matcher.find()) {

            String findContent = matcher.group();
            // 移除头部(和尾部)符号
            return findContent.substring(1, findContent.length() - 1);
        }
        return null;
    }
}
